package br.com.guarana.util;

import br.com.guarana.business.model.Parametro;
import br.com.guarana.business.service.ParametroService;
import java.io.File;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class FileDelete extends HttpServlet {

    private static final long serialVersionUID = 1L;

    public FileDelete() {
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //busca diretório para gerar arquivo temporário
        Parametro parametroSistema = new ParametroService().buscaParametroDoSistema();
        String pastaUpload = parametroSistema.getDiretorioTemp();
        String nomeArquivo = request.getParameter("nomeImagem");
        
        try {
            File f = new File(pastaUpload + "/" + nomeArquivo);
            Boolean res = f.delete();

            if (!res) {
                throw new IllegalArgumentException("Delete: Erro ao deletar arquivo");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
